package com.huilam.restactions.common.util;

import java.util.StringTokenizer;
import java.util.Vector;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class HttpUtils {
	
	public static Cookie[] findCookies(HttpServletRequest req, String aCookiePrefixName)
	{
		Vector<Cookie> vCookies = new Vector<Cookie>();
		if(aCookiePrefixName==null || aCookiePrefixName.length()==0)
			return null;
		
		Cookie[] cookies = req.getCookies();
		if(cookies!=null)
		{
			for(int i=0; i<cookies.length; i++)
			{
				if(cookies[i].getName().startsWith(aCookiePrefixName))
				{
					vCookies.add(cookies[i]);
				}
			}
			
			return vCookies.toArray(new Cookie[vCookies.size()]);
		}
		return null;
	
	}
	
	public static String[] parseUri(String aUri)
	{
		if(aUri==null)
			return null;
		
		Vector<String> vUri = new Vector<String>(); 
		StringTokenizer tk = new StringTokenizer(aUri,"/");
		while(tk.hasMoreTokens())
			vUri.add(tk.nextToken());
		return (String[])vUri.toArray(new String[vUri.size()]);
	}

	public static String parsePaths(String[] aPaths)
	{
		String sUri = "";
		
		if(aPaths==null || aPaths.length==0)
			return null;
		
		for(int i=0; i<aPaths.length; i++)
		{
			sUri += "/"+aPaths[i];
		}
		return sUri;
	}
	
	
}
